package com.hooya.fa.eu.biz.dao.manager;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.hooya.fa.eu.biz.dao.entity.*;
import com.hooya.fa.eu.biz.dao.mapper.TempKcjlDailyMapper;
import com.hooya.fa.eu.biz.dao.mapper.TradeRecordMapper;
import com.hooya.fa.eu.biz.dao.mapper.stk.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/3/18
 * @Version 1.0.0
 */
@Slf4j
@Component
@DS("fa_eu")
@AllArgsConstructor
public class AddEntityManager {

    private final TempJcjlMapper jcjlMapper;

    private final TempZyjlMapper zyjlMapper;

    private final TempKcjlDailyMapper kcjlDailyMapper;

    private final TradeRecordMapper tradeRecordMapper;

    private final TempPkpyMapper pkpyMapper;

    private final TempThjlMapper thjlMapper;

    private final TempXsjlMapper xsjlMapper;

    private final TempXsjlAbnormalMapper xsjlAbnormalMapper;

    private final TempXsjlDeleteMapper xsjlDeleteMapper;

    private final TempZyjlDeleteMapper zyjlDeleteMapper;

    private final TempZyjlChangeMapper zyjlChangeMapper;

    /**
     * 新增数据
     * kcjlDaily、TradeRecord、TempXsjl不能批量新增
     *
     * @param tempJcjlList         进仓数据
     * @param tempZyjlList         转运数据
     * @param inventoryList        仓库记录
     * @param tradeRecordList      交易记录
     * @param pkpyList             盘亏盘盈记录
     * @param tempThjlList         退货数据
     * @param tempXsjlList         销售数据
     * @param tempXsjlAbnormalList 销售拒绝数据
     * @param tempXsjlDeleteList   销售删除数据
     * @param tempZyjlDeleteList   转运删除数据
     * @param zyjlChangeList       转运更改数据
     */
    @DSTransactional
    public boolean addStkData(List<TempJcjl> tempJcjlList, List<TempZyjl> tempZyjlList,
                              List<TempKcjlDaily> inventoryList, List<TradeRecord> tradeRecordList,
                              List<TempPkpy> pkpyList, List<TempThjl> tempThjlList,
                              List<TempXsjl> tempXsjlList, List<TempXsjlAbnormal> tempXsjlAbnormalList,
                              List<TempXsjlDelete> tempXsjlDeleteList, List<TempZyjlDelete> tempZyjlDeleteList,
                              List<TempZyjlChange> zyjlChangeList) {
        //校验
        int zyNum = 0, kcNum = 0, xsNum = 0, mmNum = 0;

        if (!CollectionUtils.isEmpty(tempJcjlList)) {
            jcjlMapper.insertBatchSomeColumn(tempJcjlList);
        }
        if (!CollectionUtils.isEmpty(pkpyList)) {
            pkpyMapper.insertBatchSomeColumn(pkpyList);
        }
        if (!CollectionUtils.isEmpty(tempThjlList)) {
            thjlMapper.insertBatchSomeColumn(tempThjlList);
        }
        if (!CollectionUtils.isEmpty(tempXsjlAbnormalList)) {
            xsjlAbnormalMapper.insertBatchSomeColumn(tempXsjlAbnormalList);
        }
        if (!CollectionUtils.isEmpty(tempXsjlDeleteList)) {
            xsjlDeleteMapper.insertBatchSomeColumn(tempXsjlDeleteList);
        }
        if (!CollectionUtils.isEmpty(tempZyjlDeleteList)) {
            zyjlDeleteMapper.insertBatchSomeColumn(tempZyjlDeleteList);
        }
        if (!CollectionUtils.isEmpty(zyjlChangeList)) {
            zyjlChangeMapper.insertBatchSomeColumn(zyjlChangeList);
        }
        if (!CollectionUtils.isEmpty(inventoryList)) {
            //库存
            int partialKcLimit = inventoryList.size() / 4000 + 1;
            int kcLimit = (inventoryList.size() + partialKcLimit - 1) / partialKcLimit;
            List<List<TempKcjlDaily>> kcList = Stream.iterate(0, n -> n + 1)
                    .limit(partialKcLimit).parallel()
                    .map(a -> inventoryList.stream().skip((long) a * kcLimit).limit(kcLimit)
                            .parallel().collect(Collectors.toList())).collect(Collectors.toList());
            for (List<TempKcjlDaily> tempKcjlDailies : kcList) {
                kcjlDailyMapper.insertBatchSomeColumn(tempKcjlDailies);
                kcNum += tempKcjlDailies.size();
            }
        }
        if (!CollectionUtils.isEmpty(tradeRecordList)) {
            //买卖
            int partialLimit = tradeRecordList.size() / 2000 + 1;
            int tradeLimit = (tradeRecordList.size() + partialLimit - 1) / partialLimit;
            List<List<TradeRecord>> tradeList = Stream.iterate(0, n -> n + 1)
                    .limit(partialLimit).parallel()
                    .map(a -> tradeRecordList.stream().skip((long) a * tradeLimit).limit(tradeLimit)
                            .parallel().collect(Collectors.toList())).collect(Collectors.toList());
            for (List<TradeRecord> list : tradeList) {
                tradeRecordMapper.insertBatchSomeColumn(list);
                mmNum += list.size();
            }
        }
        if (!CollectionUtils.isEmpty(tempXsjlList)) {
            //销售
            int partialLimit = tempXsjlList.size() / 2000 + 1;
            int xsLimit = (tempXsjlList.size() + partialLimit - 1) / partialLimit;
            List<List<TempXsjl>> xsList = Stream.iterate(0, n -> n + 1)
                    .limit(partialLimit).parallel()
                    .map(a -> tempXsjlList.stream().skip((long) a * xsLimit).limit(xsLimit)
                            .parallel().collect(Collectors.toList())).collect(Collectors.toList());
            for (List<TempXsjl> list : xsList) {
                xsjlMapper.insertBatchSomeColumn(list);
                xsNum += list.size();
            }
        }
        if (!CollectionUtils.isEmpty(tempZyjlList)) {
            //转运
            int partialLimit = tempZyjlList.size() / 2000 + 1;
            int zyLimit = (tempZyjlList.size() + partialLimit - 1) / partialLimit;
            List<List<TempZyjl>> zyList = Stream.iterate(0, n -> n + 1)
                    .limit(partialLimit).parallel()
                    .map(a -> tempZyjlList.stream().skip((long) a * zyLimit).limit(zyLimit)
                            .parallel().collect(Collectors.toList())).collect(Collectors.toList());
            for (List<TempZyjl> tempZyjls : zyList) {
                zyjlMapper.insertBatchSomeColumn(tempZyjls);
                zyNum += tempZyjls.size();
            }
        }
        return kcNum == inventoryList.size() && mmNum == tradeRecordList.size() &&
                xsNum == tempXsjlList.size() && zyNum == tempZyjlList.size();
    }

	@DSTransactional
	public boolean addStkDataDaily(List<TempJcjl> tempJcjlList, List<TempZyjl> tempZyjlList,
							  List<TempKcjlDaily> inventoryList, List<TradeRecord> tradeRecordList,
							  List<TempPkpy> pkpyList, List<TempThjl> tempThjlList,
							  List<TempXsjl> tempXsjlList, List<TempXsjlAbnormal> tempXsjlAbnormalList,
							  List<TempXsjlDelete> tempXsjlDeleteList, List<TempZyjlDelete> tempZyjlDeleteList,
							  List<TempZyjlChange> zyjlChangeList) {
		//校验
		int zyNum = 0, kcNum = 0, xsNum = 0, mmNum = 0;

		if (!CollectionUtils.isEmpty(tempJcjlList)) {
			jcjlMapper.insertBatchSomeColumnDaily(tempJcjlList);
		}
		if (!CollectionUtils.isEmpty(pkpyList)) {
			pkpyMapper.insertBatchSomeColumnDaily(pkpyList);
		}
		if (!CollectionUtils.isEmpty(tempThjlList)) {
			thjlMapper.insertBatchSomeColumnDaily(tempThjlList);
		}
		if (!CollectionUtils.isEmpty(tempXsjlAbnormalList)) {
			xsjlAbnormalMapper.insertBatchSomeColumnDaily(tempXsjlAbnormalList);
		}
		if (!CollectionUtils.isEmpty(tempXsjlDeleteList)) {
			xsjlDeleteMapper.insertBatchSomeColumnDaily(tempXsjlDeleteList);
		}
		if (!CollectionUtils.isEmpty(tempZyjlDeleteList)) {
			zyjlDeleteMapper.insertBatchSomeColumnDaily(tempZyjlDeleteList);
		}
		if (!CollectionUtils.isEmpty(zyjlChangeList)) {
			zyjlChangeMapper.insertBatchSomeColumnDaily(zyjlChangeList);
		}
		if (!CollectionUtils.isEmpty(inventoryList)) {
			//库存
			int partialKcLimit = inventoryList.size() / 4000 + 1;
			int kcLimit = (inventoryList.size() + partialKcLimit - 1) / partialKcLimit;
			List<List<TempKcjlDaily>> kcList = Stream.iterate(0, n -> n + 1)
					.limit(partialKcLimit).parallel()
					.map(a -> inventoryList.stream().skip((long) a * kcLimit).limit(kcLimit)
							.parallel().collect(Collectors.toList())).collect(Collectors.toList());
			for (List<TempKcjlDaily> tempKcjlDailies : kcList) {
				kcjlDailyMapper.insertBatchSomeColumnDaily(tempKcjlDailies);
				kcNum += tempKcjlDailies.size();
			}
		}
		if (!CollectionUtils.isEmpty(tradeRecordList)) {
			//买卖
			int partialLimit = tradeRecordList.size() / 2000 + 1;
			int tradeLimit = (tradeRecordList.size() + partialLimit - 1) / partialLimit;
			List<List<TradeRecord>> tradeList = Stream.iterate(0, n -> n + 1)
					.limit(partialLimit).parallel()
					.map(a -> tradeRecordList.stream().skip((long) a * tradeLimit).limit(tradeLimit)
							.parallel().collect(Collectors.toList())).collect(Collectors.toList());
			for (List<TradeRecord> list : tradeList) {
				tradeRecordMapper.insertBatchSomeColumnDaily(list);
				mmNum += list.size();
			}
		}
		if (!CollectionUtils.isEmpty(tempXsjlList)) {
			//销售
			int partialLimit = tempXsjlList.size() / 2000 + 1;
			int xsLimit = (tempXsjlList.size() + partialLimit - 1) / partialLimit;
			List<List<TempXsjl>> xsList = Stream.iterate(0, n -> n + 1)
					.limit(partialLimit).parallel()
					.map(a -> tempXsjlList.stream().skip((long) a * xsLimit).limit(xsLimit)
							.parallel().collect(Collectors.toList())).collect(Collectors.toList());
			for (List<TempXsjl> list : xsList) {
				xsjlMapper.insertBatchSomeColumnDaily(list);
				xsNum += list.size();
			}
		}
		if (!CollectionUtils.isEmpty(tempZyjlList)) {
			//转运
			int partialLimit = tempZyjlList.size() / 2000 + 1;
			int zyLimit = (tempZyjlList.size() + partialLimit - 1) / partialLimit;
			List<List<TempZyjl>> zyList = Stream.iterate(0, n -> n + 1)
					.limit(partialLimit).parallel()
					.map(a -> tempZyjlList.stream().skip((long) a * zyLimit).limit(zyLimit)
							.parallel().collect(Collectors.toList())).collect(Collectors.toList());
			for (List<TempZyjl> tempZyjls : zyList) {
				zyjlMapper.insertBatchSomeColumnDaily(tempZyjls);
				zyNum += tempZyjls.size();
			}
		}
		return kcNum == inventoryList.size() && mmNum == tradeRecordList.size() &&
				xsNum == tempXsjlList.size() && zyNum == tempZyjlList.size();
	}

    /**
     * 为数据处理时间，并汇总数据
     *
     * @param createTime                创建时间
     * @param curDate                   当天时间
     * @param tempJcjlList              进仓集合
     * @param tempXsjlList              销售集合
     * @param tempThjlList              退货集合
     * @param tempZyjlList              转运集合
     * @param tradeRecordList           买卖集合
     * @param pkpyList                  盘亏盘盈集合
     * @param tempXsjlAbnormalList      销售不正常集合
     * @param tempXsjlDeleteList        销售删除集合
     * @param tempZyjlDeleteList        转运删除集合
     * @param finalTempJcjlList         最终进仓集合
     * @param finalTempXsjlList         最终销售集合
     * @param finalTempThjlList         最终退货集合
     * @param finalTempZyjlList         最终转运集合
     * @param finalTradeRecordList      最终买卖集合
     * @param finalPkpyList             最终盘亏盘盈集合
     * @param finalTempXsjlAbnormalList 最终销售不正常集合
     * @param finalTempXsjlDeleteList   最终销售删除集合
     * @param finalTempZyjlDeleteList   最终转运删除集合
     * @param finalZyjlChangeList       最终转运更变集合
     * @param zyjlChangeList            转运更变集合
     * @param allInventoryList          最终库存集合
     */
    public void dealDateRelation4Data(Date createTime, String curDate, List<TempJcjl> tempJcjlList,
                                      List<TempXsjl> tempXsjlList, List<TempThjl> tempThjlList,
                                      List<TempZyjl> tempZyjlList, List<TradeRecord> tradeRecordList,
                                      List<TempPkpy> pkpyList, List<TempXsjlAbnormal> tempXsjlAbnormalList,
                                      List<TempXsjlDelete> tempXsjlDeleteList, List<TempZyjlDelete> tempZyjlDeleteList,
                                      List<TempJcjl> finalTempJcjlList, List<TempXsjl> finalTempXsjlList,
                                      List<TempThjl> finalTempThjlList, List<TempZyjl> finalTempZyjlList,
                                      List<TradeRecord> finalTradeRecordList, List<TempPkpy> finalPkpyList,
                                      List<TempXsjlAbnormal> finalTempXsjlAbnormalList,
                                      List<TempXsjlDelete> finalTempXsjlDeleteList,
                                      List<TempZyjlDelete> finalTempZyjlDeleteList,
                                      List<TempZyjlChange> finalZyjlChangeList, List<TempZyjlChange> zyjlChangeList,
                                      List<TempKcjlDaily> allInventoryList) {
        tempZyjlList.forEach(e -> {
            e.setCreateTime(createTime);
            if (StringUtils.isBlank(e.getHthm())) {
                e.setHthm("");
            }
        });
        tradeRecordList.forEach(e -> {
            e.setCreateTime(createTime);
            e.setDate(curDate);
        });
        pkpyList.forEach(e -> {
            e.setDate(curDate);
            e.setCreateTime(createTime);
        });
        tempJcjlList.forEach(e -> e.setCreateTime(createTime));
        tempXsjlList.forEach(e -> e.setCreateTime(createTime));
        tempThjlList.forEach(e -> e.setCreateTime(createTime));
        tempXsjlAbnormalList.forEach(e -> e.setCreateTime(createTime));
        tempXsjlDeleteList.forEach(e -> e.setCreateTime(createTime));
        tempZyjlDeleteList.forEach(e -> e.setCreateTime(createTime));
        allInventoryList.forEach(e -> e.setCreateTime(createTime));


        //汇总数据
        finalTempJcjlList.addAll(tempJcjlList);
        finalTempXsjlList.addAll(tempXsjlList);
        finalTempThjlList.addAll(tempThjlList);
        finalTempZyjlList.addAll(tempZyjlList);
        finalTradeRecordList.addAll(tradeRecordList);
        finalPkpyList.addAll(pkpyList);
        finalTempXsjlAbnormalList.addAll(tempXsjlAbnormalList);
        finalTempXsjlDeleteList.addAll(tempXsjlDeleteList);
        finalTempZyjlDeleteList.addAll(tempZyjlDeleteList);
        finalZyjlChangeList.addAll(zyjlChangeList);
    }
}
